/* poporcx.p - PURCHASE ORDER RECEIPT W/ SERIAL NUMBER CONTROL               */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: poporcx.p 85749 2019-04-25 02:34:43Z cfs                          $: */
/* REVISION: 7.4            CREATED: 11/04/93   BY: bcm *H210*               */
/*                                   09/08/94   BY: bcm *H509*               */
/*                                   09/09/94   BY: bcm *H511*               */
/*           7.4                     10/31/94   BY: ame *GN82*               */
/*           7.4                     11/17/94   BY: bcm *GO37*               */
/*           7.4                     11/22/94   BY: bcm *H606*               */
/* REVISION: 8.5           MODIFIED: 10/13/95   BY: taf *J053*               */
/* REVISION: 8.5      LAST MODIFIED: 09/10/96   BY: *H0MP* Aruna P.Patil     */
/* REVISION: 8.5      LAST MODIFIED: 01/29/97   BY: *H0RX* Sue Poland        */
/* REVISION: 8.5      LAST MODIFIED: 02/20/97   BY: *H0SL* Jim Williams      */
/* REVISION: 8.6      LAST MODIFIED: 10/09/97   BY: *K0JV* Surendra Kumar    */
/* REVISION: 8.6E     LAST MODIFIED: 05/09/98   BY: *L00Y* Jeff Wootton      */
/* REVISION: 8.6E     LAST MODIFIED: 06/11/98   BY: *L020* Charles Yen       */
/* REVISION: 8.6E     LAST MODIFIED: 08/19/98   BY: *J2WP* Irine D'mello     */
/* REVISION: 8.6E     LAST MODIFIED: 12/01/98   BY: *L0CN* Steve Goeke       */
/* REVISION: 9.0      LAST MODIFIED: 03/22/99   BY: *J37F* Poonam Bahl       */
/* REVISION: 9.0      LAST MODIFIED: 04/16/99   BY: *J2DG* Reetu Kapoor      */
/* REVISION: 9.1      LAST MODIFIED: 10/01/99   BY: *N014* PATTI GAULTNEY    */
/* REVISION: 9.1      LAST MODIFIED: 02/24/00   BY: *M0K0* Ranjit Jain       */
/* Revision: 1.21    BY: Jack Rief      DATE: 06/13/00 ECO: *N0DK*           */
/* Revision: 1.22    BY: myb            DATE: 08/13/00 ECO: *N0KQ*           */
/* Revision: 1.23    BY: Murali Ayyagari DATE: 12/04/00 ECO: *N0V1*          */
/* Old ECO marker removed, but no ECO header exists *F0PN*                   */
/* Revision: 1.24          BY: Katie Hilbert      DATE: 04/01/01  ECO: *P002* */
/* Revision: 1.25          BY: Patrick Rowan      DATE: 04/17/02  ECO: *P043* */
/* Revision: 1.27          BY: Paul Donnelly (SB) DATE: 06/28/03  ECO: *Q00J* */
/* Revision: 1.28          BY: Rajaneesh S.       DATE: 01/08/04  ECO: *P1GK* */
/* Revision: 1.29          BY: Paul Dreslinski    DATE: 10/30/04  ECO: *M1M3* */
/* Revision: 1.30          BY: Tejasvi Kulkarni   DATE: 06/30/05  ECO: *P3VR* */
/* Revision: 1.31          BY: Preeti Sattur      DATE: 06/12/06  ECO: *P4SL* */
/* Revision: 1.32          BY: Archana Kirtane    DATE: 04/27/07  ECO: *P5VP* */
/* Revision: 1.33          BY: Neil Curzon        DATE: 09/08/08  ECO: *R13L* */
/*-Revision end---------------------------------------------------------------*/
 
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
 
/* ADDED NO-UNDO AND ASSIGN STATEMENT WHEREVER MISSING          */
/* REPLACED FIND STATEMENTS WITH FOR FIRST STATEMENTS           */
/* FOR ORACLE PERFORMANCE                                       */
 
/*****************************************************************************/
/*      SUB-ACCOUNT FIELD ADDED; WILL BE USED IN CONJUNCTION WITH ACCT AND   */
/*      COST CENTER. SUB-ACCOUNT IS NO LONGER CONCATENATED TO ACCT AND IS A  */
/*      SEPARATE 8 CHARACTER FIELD. CHANGED RUN MODE TOKEN.                  */
/*****************************************************************************/
 
{us/bbi/mfdeclre.i}
{us/gp/gpuid.i}
{us/px/pxmaint.i}
 
/* Define Handles for the programs. */
{us/px/pxphdef.i porcxr}
{us/px/pxphdef.i porcxr1}
{us/px/pxphdef.i txtxxr}
/* End Define Handles for the programs. */
 
define input parameter tax_lines like tx2d_line  no-undo.
define input parameter p_shipnbr like tr_ship_id no-undo. 
define input parameter p_ShipperReceipt as character no-undo.
 
/* AND NOT JUST VIEWED IN DR/CR MEMO MAINTENANCE                   */
define new shared variable l_txchg     like mfc_logical initial no.
 
define shared variable rndmthd       like rnd_rnd_mthd.
define shared variable stdcst        like tr_price.
define shared variable eff_date      like glt_effdate.
define shared variable po_recno      as   recid.
define shared variable ps_nbr        like prh_ps_nbr.
define shared variable receivernbr   like prh_receiver.
define shared variable site          like sod_site no-undo.
define shared variable gl_amt        like trgl_gl_amt extent 7.
define shared variable dr_acct       like trgl_dr_acct extent 7.
define shared variable dr_sub        like trgl_dr_sub extent 7.
define shared variable dr_cc         like trgl_dr_cc extent 7.
define shared variable dr_proj       like trgl_dr_proj extent 7.
define shared variable cr_acct       like trgl_cr_acct extent 7.
define shared variable cr_sub        like trgl_cr_sub extent 7.
define shared variable cr_cc         like trgl_cr_cc extent 7.
define shared variable cr_proj       like trgl_cr_proj extent 7.
define shared variable rcv_type      like poc_rcv_type.
define shared variable pod_recno     as   recid.
define shared variable exch_rate     like exr_rate.
define shared variable exch_rate2    like exr_rate.
define shared variable is-return     like mfc_logical no-undo.
define shared variable porec         like mfc_logical no-undo.
define shared variable shipper_rec   like mfc_logical.
define shared variable project       like pvo_project.
define shared variable tax_tr_type   like pvo_project no-undo.
 
define variable price           like tr_price no-undo.
define variable prh_ok          like mfc_logical initial false no-undo.
define variable tax-edited      like mfc_logical no-undo.
define variable tax-prompt      like mfc_logical no-undo.
define variable tax_total       as   decimal no-undo.
define variable order-tax-type  like pvo_project no-undo.
define variable comb_exch_rate  like exr_rate    no-undo.
define variable comb_exch_rate2 like exr_rate2   no-undo.
define variable mc-error-number like msg_nbr     no-undo.
define variable l_tax_date      like po_tax_date no-undo.
define variable l_abs_um_conv      as   decimal   no-undo.
define variable l_abs_um           as   character no-undo.
define variable l_total_abs_um_qty as   decimal   no-undo.
define variable l_ori_price        as   decimal   no-undo.
define variable l_total_abs_qty    as   decimal   no-undo.
define buffer   b_absmstr          for  abs_mstr.
define variable l_price_variance   as   logical   no-undo. 
define variable allowCopy          as   logical initial true no-undo.
define variable brazil_ld          as   logical initial false no-undo.

{us/tx/txcalvar.i}
 
{us/tx/txcurvar.i "NEW"}
 
for first gl_ctrl  where gl_ctrl.gl_domain = global_domain no-lock:
end. /* FOR FIRST GL_CTRL */
 
for first po_mstr
   where recid(po_mstr) = po_recno 
no-lock:
end. /* FOR FIRST PO_MSTR */
 
for first poc_ctrl
   where poc_ctrl.poc_domain = global_domain 
no-lock:
end. /* FOR FIRST POC_CTRL */
 
assign rcv_type = poc_rcv_type.
 
/* SET ORDER-TAX-TYPE BASED ON ORDER TYPE TO ALLOW
COPY OF EDITED TAX DETAILS FOR RTS'S AND PO'S */
{us/px/pxrun.i &PROC = 'getOrderTaxType' &PROGRAM = 'porcxr.p'
   &HANDLE=ph_porcxr
   &PARAM = "(input po_fsm_type,
              output order-tax-type)"
   &CATCHERROR = true
   &NOAPPERROR = true
   }
 
assign
   tax-edited = no
   tax-prompt = yes.
 
podloop:
for each pod_det no-lock  where pod_det.pod_domain = global_domain and (
      pod_nbr = po_nbr and
      (pod_line = tax_lines or tax_lines = 0) and
      ((is-return and pod_qty_chg <> 0) or
      ((not is-return) and
      pod_status <> "c" and
      pod_status <> "x" and
      (pod_qty_chg <> 0 or pod_bo_chg = 0) and
      ((not porec and pod_rma_type <> "I" and pod_rma_type <> "") or
      (porec and pod_rma_type <> "O")) and
      ((shipper_rec and pod_qty_chg <> 0 ) or not shipper_rec) and
      ((pod_sched and pod_qty_chg <> 0 ) or not pod_sched))  )
      ) break by pod_part:
 
 
   {us/px/pxrun.i &PROC = 'calculateUnitCost' &PROGRAM = 'porcxr1.p'
                  &HANDLE=ph_porcxr1
                  &PARAM="(buffer pod_det,
                           output price)"
                  &CATCHERROR = true
                  &NOAPPERROR = true
   }
 
   if pod_qty_chg <> 0 then do:
 
      assign 
         prh_ok             = yes
         l_abs_um           = pod_um
         l_abs_um_conv      = pod_um_conv.
 
      if shipper_rec
      then do:
         assign
            l_total_abs_qty    = 0
            l_total_abs_um_qty = 0
            l_price_variance   = no
            l_ori_price        = price * pod_um_conv.
 
         for each b_absmstr
            where b_absmstr.abs_domain                 = global_domain
            and   b_absmstr.abs_order                  = pod_nbr
            and   int(b_absmstr.abs_line)              = pod_line
            and   b_absmstr.abs_shipfrom               = po_vend
            and   substring(b_absmstr.abs_par_id,2,20) = p_shipnbr
         no-lock:
            
            if pod_um = b_absmstr.abs__qad02 
               and pod_um_conv <> decimal(b_absmstr.abs__qad03) 
            then
               l_price_variance = yes. 
             
            l_total_abs_qty    = l_total_abs_qty
               + (b_absmstr.abs_qty - b_absmstr.abs_ship_qty).
 
            if decimal(b_absmstr.abs__qad03) <> 0
            then
               assign
                  l_abs_um           = if b_absmstr.abs__qad02 <> pod_um
                                       then
                                          b_absmstr.abs__qad02
                                       else
                                          l_abs_um
                  l_total_abs_um_qty = l_total_abs_um_qty
                     + ( (b_absmstr.abs_qty - b_absmstr.abs_ship_qty)
                     / decimal(b_absmstr.abs__qad03) ).
         end. /* FOR EACH b_absmstr */
         if l_total_abs_um_qty <> 0
         then
            l_abs_um_conv = l_total_abs_qty / l_total_abs_um_qty.
         if l_abs_um_conv <> 0
            and l_price_variance
         then
            price = l_ori_price / l_abs_um_conv.
      end. /* IF shipper_rec */
 
      /* Set the site variable to the value in sr_wkfl for the */
      /* purchase order receipt in order to ensure that the    */
      /* temporary history record uses the correct site.       */
      for first sr_wkfl where sr_domain = global_domain
                          and sr_userid = SessionUniqueID
                          and sr_lineid = string(pod_line)
      no-lock:
         assign site = sr_site.
      end. 
      {us/px/pxrun.i &PROC = 'createTempHistoryRecords' &PROGRAM = 'porcxr1.p'
         &HANDLE=ph_porcxr1
         &PARAM = "(buffer pod_det,
                    input site,
                    input eff_date,
                    input receivernbr,
                    input price,
                    input ps_nbr,
                    input dr_acct[1],
                    input dr_sub[1],
                    input dr_cc[1],
                    input project,
                    input stdcst,
                    input exch_rate,
                    input exch_rate2,
                    input l_abs_um,
                    input l_abs_um_conv,
                    input l_total_abs_um_qty,
                    input shipper_rec,
                    input p_ShipperReceipt,
                    input yes)"
         &CATCHERROR = true
         &NOAPPERROR = true
         }
 
   end.
 
end.
 
if prh_ok then do transaction:
 
   /* CHECK FOR FIRST EDITED TAX DETAIL FOR THIS SELECTION */
 
   /* ADDED TWO PARAMETERS: INPUT PARAMETER VQ-POST
   * AND OUTPUT PARAMETER RESULT-STATUS. THE POST FLAG IS SET
   * TO 'NO' BECAUSE WE ARE NOT CREATING QUANTUM REGISTER
   * RECORDS FROM THIS CALL TO TXCALC.P INSIDE calculateTax. */
   {us/px/pxrun.i &PROC='calculateTax' &PROGRAM='txtxxr.p'
            &HANDLE=ph_txtxxr
            &PARAM="(input tax_tr_type,
                     input receivernbr,
                     input po_nbr,
                     input tax_lines,
                     input no,
                     input no,
                     output result-status)"
            &NOAPPERROR=true
            &CATCHERROR=true
    }
 
   /* Call hook program to reverse the sign for amounts in tx2d_det */
   /* for tr type = "17".                                           */
   {us/bbi/gprun.i ""gppordat2.p"" "(input po_nbr,
                                     input receivernbr)"} 

   if po_vend <> "" and p_shipnbr <> "" then do:
      /* Get legal document form code */
      {us/gp/gprunp.i "soldxr" "p" "isBrazilStyleLD"
                      "(input  po_vend,
                        input  's' + p_shipnbr,
                        output brazil_ld)"}
   end.

   if brazil_ld then
      run allowCopyTaxesAbs(input po_vend,
                            input p_shipnbr,
                            output allowCopy).

   if tax-prompt and not tax-edited and allowCopy then do:
      {us/px/pxgetph.i txtxxr}
      assign
         tax-edited = {us/px/pxfunct.i &FUNCTION='isTaxLineEdited' &PROGRAM='txtxxr.p'
                                 &HANDLE=ph_txtxxr
                                 &PARAM = "input po_nbr,
                                           input tax_lines,
                                           input po_blanket,
                                           input order-tax-type"
                      }.
 
      if tax-prompt and tax-edited then do:
         {us/mf/mfmsg01.i 935 2 tax-edited}
         assign tax-prompt = no.
      end.
   end.
   /* COPY EDITED RECORDS IF SPECIFIED BY USER */
   if tax-edited then do:
      {us/px/pxrun.i &PROC='copyTaxValues' &PROGRAM='txtxxr.p'
               &HANDLE=ph_txtxxr
               &PARAM="(input order-tax-type,
                        input  po_nbr,
                        input  po_blanket,
                        input  tax_tr_type,
                        input  receivernbr,
                        input  po_nbr,
                        input  tax_lines)"
               &CATCHERROR = true
               &NOAPPERROR = true
      }
 
      if po_tax_date <> ?
      then
         l_tax_date = po_tax_date.
      else
      if po_due_date <> ?
      then
         l_tax_date = po_due_date.
      else
         l_tax_date = po_ord_date.
   end. /* IF tax-edited THEN */
 
   /* ADDED THIS LOGIC FOR PO RETURNS, IN ORDER TO ALLOW THE USER TO */
   /* VIEW/EDIT THE TAX WHEN THERE EXISTS EDITED PO RECEIPTS         */
 
   if not tax-edited
      and (execname = "porvis.p" or execname = "paporvpk.p")
 
     then do:
      for each prh_hist
         where prh_domain = global_domain
         and   prh_nbr    = po_nbr
      no-lock:
         if can-find (first tx2d_det
            where tx2d_domain  = global_domain
            and   tx2d_ref     = prh_receiver
            and   tx2d_nbr     = po_nbr
            and   tx2d_tr_type = "21"
            and   tx2d_edited)
         then do:
 
            /* VIEW/EDIT TAX DETAIL */
            {us/bbi/pxmsg.i &MSGNUM  = 932
                     &CONFIRM = "tax-edited"}
            leave.
         end. /* IF CAN-FIND(tx2d_det ... */
      end. /* FOR EACH prh_hist */
   end. /* IF NOT tax-edited AND execname = "porvis.p" */
 
   if tax-edited
   then do:
      /* ADDED po_curr,po_ex_ratetype,po_ex_rate,po_ex_rate2  */
      /* AND l_tax_date AS SIXTH, SEVENTH, EIGTH, NINTH       */
      /* AND TENTH INPUT PARAMETER RESPECTIVELY.              */
 
      do on error undo, return error return-value:
         {us/bbi/gprun.i ""txedit.p""
            "(input  tax_tr_type,
              input  receivernbr,
              input  po_nbr,
              input  tax_lines, /*ALL LINES*/
              input  po_tax_env,
              input  po_curr,
              input  po_ex_ratetype,
              input  po_ex_rate,
              input  po_ex_rate2,
              input  l_tax_date,
              output tax_total)"}
      end.
   end. /* IF tax-edited THEN */
{us/px/pxrun.i &PROC='deleteTempHistoryRecords' &PROGRAM='porcxr1.p'
         &HANDLE=ph_porcxr1
         &PARAM="(input po_nbr,
                  input receivernbr,
                  input tax_lines)"
         &CATCHERROR = true
         &NOAPPERROR = true
}
 
end.

PROCEDURE allowCopyTaxesAbs:
   define input parameter i_shipfrom like abs_mstr.abs_shipfrom no-undo.
   define input parameter i_abs_par_id like abs_mstr.abs_par_id no-undo.
   define output parameter o_allowcopy as logical initial yes no-undo.

   define variable i as integer no-undo.    

   define buffer lgdc_ctrl for lgdc_ctrl.
   define buffer work_abs_mstr for abs_mstr.

   for first lgdc_ctrl no-lock
      where lgdc_ctrl.lgdc_domain = global_domain
        and lgdc_ctrl.lgdc_type   = "ISS":
   end. /* lgdc_ctrl */   

   if not available lgdc_ctrl then return.

   /* if Consolidate LD lines is no and shipper was created with multi-entry *
    * = yes, then not allow tax copy                                         */
   if lgdc_consol_lines = no then do:
      for each work_abs_mstr no-lock
         where work_abs_mstr.abs_domain = global_domain
           and work_abs_mstr.abs_shipfrom = i_shipfrom 
           and substring(work_abs_mstr.abs_par_id,2,20) = i_abs_par_id
         break by abs_order
               by abs_line:
         if first-of(abs_line) then i = 0.
         i = i + 1.
         if i = 2 then leave.
      end.
      if i = 2 then o_allowcopy = no.
   end.
   
END PROCEDURE.
